//
//  SJPopoverPresentationController.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/10/4.
//  Copyright © 2015年 world. All rights reserved.
//
//  自定义返回modal 动画的对象

import UIKit

class SJPopoverPresentationController: UIPresentationController {
    
    ///展现视图的大小
    var presentedFrame = CGRectZero
    
    ///遮罩视图
    lazy var coverView:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        //创建单击手势
        let tap = UITapGestureRecognizer(target: self, action: "close")
        v.addGestureRecognizer(tap)

        return v
    }()
    
//    /// 准备遮罩
//    private func prepareCoverView(){
//        //创建单击手势
//        let tap = UITapGestureRecognizer(target: self, action: "close")
//        coverView.addGestureRecognizer(tap)
//    }
    ///单击手势回调方法, 关闭当前被展现出来的控制器 也就是presentedViewController
    func close(){
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    ///实例化转场动画
    ///presentedViewController  被展现出来的控制器
    ///presentingViewController 文档上说:发现的控制器,但是Xcode6是nil,Xcode7野指针
//    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
//            super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
//        
//        prepareCoverView()
//    }
    ///containerView 容器视图,用来放置展现出来的视图的
    ///presentedView() 被展现出来的视图
    ///containerViewWillLayoutSubviews 容器视图将要重新布局子视图
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        //添加设置遮罩
        containerView?.insertSubview(coverView, atIndex: 0)
        coverView.frame = containerView!.bounds
        
        presentedView()?.frame = presentedFrame
    }
}
